package edu.unlv.cs.ladders.services;

import javax.persistence.Query;

import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.unlv.cs.ladders.entities.*;

@Service
@Transactional(readOnly = true)
public class JPAUserDetailsService implements UserDetailsService {
	private static final boolean ENABLED = true;
	private static final boolean ACCOUNT_NON_EXPIRED = true;
	private static final boolean ACCOUNT_NON_LOCKED = true;
	private static final boolean CREDENTIALS_NON_EXPIRED = true;
	
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {
		Query query = Participant
			.findParticipantsByEnabledAndAccountNonExpiredAndAccountNonLockedAndCredentialsNonExpiredAndUsernameEquals(
					ENABLED, ACCOUNT_NON_EXPIRED, ACCOUNT_NON_LOCKED, CREDENTIALS_NON_EXPIRED, username);
		Participant participant = (Participant)query.getSingleResult();
		return participant;
	}
}
